package indi.zhifa.recipe.bailan.framework.auth.handler;

import indi.zhifa.recipe.bailan.framework.auth.entity.BaseTokenObject;
import indi.zhifa.recipe.bailan.framework.auth.util.IBaseTokenUtil;
import indi.zhifa.recipe.bailan.framework.mysql.handler.BaseMetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;


@Slf4j
public abstract class DefaultMetaObjectHandler extends BaseMetaObjectHandler {
    private final IBaseTokenUtil mTokenUtil;

    protected DefaultMetaObjectHandler(IBaseTokenUtil pTokenUtil){
        mTokenUtil = pTokenUtil;
    }

    @Override
    protected void insertCreateBy(MetaObject pMetaObject){
        BaseTokenObject tokenObject = mTokenUtil.getTokenObject();
        if(pMetaObject.hasSetter(createByStr)){
            Object orgCreateBy = pMetaObject.getValue(createByStr);
            if(null == orgCreateBy){
                final Long id;
                if(null != tokenObject){
                    id = tokenObject.getId();
                }else{
                    id = SYS_ID;
                }
                this.strictInsertFill(pMetaObject,createByStr,()->id,Long.class);
            }
        }
    }

    @Override
    protected void insertCreateByName(MetaObject pMetaObject){
        BaseTokenObject tokenObject = mTokenUtil.getTokenObject();
        if(pMetaObject.hasSetter(createByNameStr)){
            Object createByName = pMetaObject.getValue(createByNameStr);
            if(null == createByName){
                final String name;
                if(null != tokenObject){
                    name = tokenObject.getName();
                }else{
                    name = SYS_NAME;
                }
                this.strictInsertFill(pMetaObject, createByNameStr,()->name,String.class);
            }
        }
    }

    @Override
    protected void insertCreateByIp(MetaObject pMetaObject){
        BaseTokenObject tokenObject = mTokenUtil.getTokenObject();
        if(pMetaObject.hasSetter(createIpStr)){
            Object createIp = pMetaObject.getValue(createIpStr);
            if(null == createIp){
                final String ip;
                if(null != tokenObject){
                    ip = tokenObject.getRemoteIp();
                }else{
                    ip = SYS_IP;
                }
                this.strictInsertFill(pMetaObject,createIpStr,()->ip,String.class);
            }
        }
    }

    @Override
    protected void updateModifyBy(MetaObject pMetaObject){
        BaseTokenObject tokenObject = mTokenUtil.getTokenObject();
        if(pMetaObject.hasSetter(modifyByStr)){
            Object orgUpdateBy = pMetaObject.getValue(modifyByStr);
            if(null == orgUpdateBy){
                final Long id;
                if(null != tokenObject){
                    id = tokenObject.getId();
                }else{
                    id = SYS_ID;
                }
                this.strictUpdateFill(pMetaObject,modifyByStr,()->id,Long.class);
            }
        }
    }

    @Override
    protected void updateModifyByName(MetaObject pMetaObject){
        BaseTokenObject tokenObject = mTokenUtil.getTokenObject();
        if(pMetaObject.hasSetter(modifyByNameStr)){
            Object updateByName = pMetaObject.getValue(modifyByNameStr);
            if(null == updateByName){
                final String name;
                if(null != tokenObject){
                    name = tokenObject.getName();
                }else{
                    name = SYS_NAME;
                }
                this.strictUpdateFill(pMetaObject, modifyByNameStr,()->name,String.class);
            }
        }
    }

    @Override
    protected void updateModifyByIp(MetaObject pMetaObject){
        BaseTokenObject tokenObject = mTokenUtil.getTokenObject();
        if(pMetaObject.hasSetter(modifyIpStr)){
            Object updateIp = pMetaObject.getValue(modifyIpStr);
            if(null == updateIp){
                final String ip;
                if(null != tokenObject){
                    ip = tokenObject.getRemoteIp();
                }else{
                    ip = SYS_IP;
                }
                this.strictUpdateFill(pMetaObject,modifyIpStr,()->ip,String.class);
            }
        }
    }


    @Override
    protected boolean hasTokenObject(){
        return mTokenUtil.hasTokenObject();
    }
}
